package com.rzjm.api.util.power;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @创建者：zhouwei
 * @创建时间：2023/5/24
 * @描述：
 */
public class SmartPowerUtil {

    private final static String USERID = "bay1684913820387JWWK";

    public static void main(String[] args) {
        getAssets("");
    }

    /**
     * 查询用户下所有授权资产
     * @return
     */
    public static JSONObject getAssets(String accessToken){

        Map<String,Object> mapHeader = new HashMap<>();
        mapHeader.put("client_id","");

        String getTokenPath = "/v1.0/cloud/energy/micro/assets?user_id=" + USERID;//code就是授权信息中的授权码
//        System.out.println("getTokenPath: " + getTokenPath);
        Object result = RequestSignUtils.execute(accessToken, getTokenPath, "GET", "", new HashMap<>());
//        System.out.println("资产数据： "+result);
        return JSONUtil.parseObj(result);
    }

    /**
     *  查询用户资产下的设备列表
     * @param asset_id
     * @return
     */

    public static JSONObject getDeviceList(String asset_id,String accessToken){
        String getTokenPath = "/v1.0/cloud/energy/micro/assets/"+asset_id+"/devices?user_id=" + USERID;//code就是授权信息中的授权码
        Object result = RequestSignUtils.execute(accessToken, getTokenPath, "GET", "", new HashMap<>());
        System.out.println("获取设备列表：" + result);
        return JSONUtil.parseObj(result);
    }


    /**
     *  查询用户资产下的设备列表
     * @param device_id
     * @return
     */

    public static JSONObject getDeviceStatus(String device_id,String accessToken){
        //{{url}}/v1.0/iot-03/devices/status?user_id=******&device_ids=******这是获取单个设备状态的接口
        String getTokenPath = "/v1.0/iot-03/devices/status?user_id="+USERID+"&device_ids=" + device_id;//code就是授权信息中的授权码
        Object result = RequestSignUtils.execute(accessToken, getTokenPath, "GET", "", new HashMap<>());
//        System.out.println("获取单个设备开闸详情：" + JSONUtil.toJsonStr(result));
        return JSONUtil.parseObj(result);
    }

    /**
     *  查询用户资产下的设备列表
     * @param device_id
     * @return
     */

    public static JSONObject getDeviceDetail(String device_id,String accessToken){
        //{{url}}/v1.0/iot-03/devices/status?user_id=******&device_ids=******这是获取单个设备状态的接口
        String getTokenPath = "/v1.0/iot-03/devices/" + device_id;//code就是授权信息中的授权码
        Object result = RequestSignUtils.execute(accessToken, getTokenPath, "GET", "", new HashMap<>());
        System.out.println("获取单个设备在线详情：" + JSONUtil.toJsonStr(result));
        return JSONUtil.parseObj(result);
    }

    /**
     * 设备控制
     * @param device_id
     * @return
     */
    public static JSONObject deviceCommand(String device_id,String accessToken,boolean OpenStatus){
        String getTokenPath = "/v1.0/cloud/energy/micro/device/command/" + device_id + "?user_id=" + USERID + "&code=switch&value=" + OpenStatus;//code就是授权信息中的授权码
//        System.out.println("设备控制:" + getTokenPath);
        Object result = RequestSignUtils.execute(accessToken, getTokenPath, "POST", "", new HashMap<>());
        System.out.println("继电器控制：" + result);
        return JSONUtil.parseObj(result);
    }
}
